import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store/state'
import '@/assets/commonJs/rem.js' // rem适配
import '@/assets/style/common.less' // 通用样式
import '@/assets/style/reset.css'
import '@/assets/style/iconfont.css'
import '@/assets/mock/user/user-info.js'
import '@/assets/mock/user/logout.js'
import {
  Toast
} from 'vant'
import VueLazyload from 'vue-lazyload'
import { Component } from 'vue-property-decorator'
Component.registerHooks([
  'beforeRouteEnter',
  'beforeRouteLeave'
])
Vue.use(VueLazyload, {
  preLoad: 1.3,
  error: '../static/mock/img/error.jpg',
  // loading: require('./assets/loading.png'),
  loading: '../static/mock/img/loading.png',
  attempt: 1
})
Vue.prototype.$toast = Toast
Vue.prototype.$store = store
Vue.prototype.$publicPath = process.env.BASE_URL
Vue.config.productionTip = false
const whiteList = ['/login', '/list']
router.beforeEach((to:any, from:any, next:any) => {
  const token:String | null = localStorage.getItem('token')
  if (token) {
    if (to.name !== 'login') {
      store.dispatch('user/getUserInfo')
      next()
    } else {
      next({ name: 'mine' })
    }
  } else {
    if (whiteList.indexOf(to.path) !== -1) {
      // 在免登录白名单中，直接进入
      next()
    } else {
      next(`/login?redirect=${to.path}`)
    }
  }
})
new Vue({
  router,
  store,
  render: (h) => h(App),
}).$mount('#app')
export default Vue